package com.secsbrain.cloudconsole.biz.impl;

import java.util.Date;

import com.secsbrain.cloudconsole.biz.IPicAgreeRecordService;
import com.secsbrain.cloudconsole.dal.model.PicAgreeRecord;
import com.secsbrain.cloudconsole.dal.model.PicShare;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.secsbrain.cloudconsole.dal.mapper.PicShareMapper;
import com.secsbrain.cloudconsole.biz.IPicShareService;
import com.baomidou.framework.service.impl.SuperServiceImpl;

/**
 *
 * PicShare 表数据服务层接口实现类
 *
 */
@Service
public class PicShareServiceImpl extends SuperServiceImpl<PicShareMapper, PicShare> implements IPicShareService {
	
	@Autowired
	private IPicAgreeRecordService picAgreeRecordService;

	/* (non-Javadoc)
	 * @see IPicShareService#agree(java.lang.String, java.lang.Long)
	 */
	@Override
	@Transactional
	public void agree(String openId, Long id) {
		PicShare picShare=this.selectById(id);
		Assert.notNull(picShare, "没有查询到相关照片");
		
		PicShare picShareParam=new PicShare();
		picShareParam.setItype(picShare.getItype());
		picShareParam.setOpenId(openId);
		int count=this.selectCount(picShareParam);
		Assert.isTrue(count>0, "你还没有上传图片，不能点赞");
		
		this.baseMapper.updateAgreeCount(id);
		
		PicAgreeRecord picAgreeRecord=new PicAgreeRecord();
		picAgreeRecord.setCreateTime(new Date());
		picAgreeRecord.setItype(picShare.getItype());
		picAgreeRecord.setOpenId(openId);
		picAgreeRecord.setPicId(id);
		picAgreeRecordService.insertSelective(picAgreeRecord);
	}

}